package com.hellobike.haxing.modules.wms.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.wms.entity.Imperfections;
import com.hellobike.haxing.modules.wms.enums.ImperfectionStatusEnum;
import com.hellobike.haxing.modules.wms.exception.WmsServiceException;
import com.hellobike.haxing.modules.wms.service.IImperfectionsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.hellobike.haxing.common.annotation.AutoLog;
 /**
 * @Description: t_wms_imperfections
 * @Author: haxing-boot
 * @Date:   2020-02-19
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/imperfections")
@Slf4j
public class ImperfectionsController extends WmsBaseController<Imperfections, IImperfectionsService> {
	@Autowired
	private IImperfectionsService imperfectionsService;
	@Autowired
	private ITSupplierInfoService supplierInfoService;
	
	/**
	 * 分页列表查询
	 *
	 * @param imperfections
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "t_wms_imperfections-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Imperfections imperfections,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		imperfections.setWarehouseId(getWarehouseId());
		QueryWrapper<Imperfections> queryWrapper = QueryGenerator.initQueryWrapper(imperfections, req.getParameterMap());
		Page<Imperfections> page = new Page<Imperfections>(pageNo, pageSize);
		IPage<Imperfections> pageList = imperfectionsService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param imperfections
	 * @return
	 */
	@AutoLog(value = "t_wms_imperfections-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Imperfections imperfections) {
		if(null != imperfections.getSupplierId()){
			TSupplierInfo supplier = supplierInfoService.getById(imperfections.getSupplierId());
			if (supplier != null) {
				imperfections.setSupplierName(supplier.getSupplierName());
			}
		}
		imperfections.setImperfectionStatus(ImperfectionStatusEnum.INIT.getValue());
		imperfections.setWarehouseId(getWarehouseId());
		imperfectionsService.save(imperfections);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param imperfections
	 * @return
	 */
	@AutoLog(value = "t_wms_imperfections-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Imperfections imperfections) {

		Imperfections byId = imperfectionsService.getById(imperfections.getId());
		if(!ImperfectionStatusEnum.INIT.getValue().equals(byId.getImperfectionStatus())){
			return Result.error("已处理残次不可再次变更");
		}
		imperfectionsService.updateById(imperfections);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_wms_imperfections-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		imperfectionsService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_wms_imperfections-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.imperfectionsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Imperfections imperfections = imperfectionsService.getById(id);
		if(imperfections==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(imperfections);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param imperfections
    */
    @AutoLog(value = "t_wms_imperfections-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Imperfections imperfections) {
        return super.exportXls(request, imperfections, Imperfections.class, "t_wms_imperfections");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "t_wms_imperfections-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Imperfections.class);
    }


	 @AutoLog(value = "t_wms_imperfections-审核",operateType=3)
	 @PutMapping(value = "/audit")
	 public Result<?> audit(@RequestBody Imperfections imperfections) throws WmsServiceException {
		 imperfectionsService.audit(imperfections.getId(),imperfections.getImperfectionStatus(),getWarehouseId(),getSysUserId());
		 return Result.ok("审核成功!");
	 }

}
